﻿using System.Linq;
using System.Collections.Generic;
using System.Web.Mvc;
using Mvcmediaplayer.Models;
using Mvcmediaplayer.ViewModels;

namespace Mvcmediaplayer.Controllers
{
    [HandleError]
    [Authorize(Roles="Administrator")]
    public class VideoManagerController : Controller
    {
        mediaEntities mediaDB = new mediaEntities();
        //
        // GET: /VideoManager/

        public ActionResult Index()
        {
            var videos = mediaDB.Videos.OrderBy(v=>v.Id).Take(5).ToList();
           
            return View(videos);
        }

        [HttpPost]
        public string GetData(int pageIndex)
        {
            var html = "";
            var videos = mediaDB.Videos.OrderBy(v => v.Id).Skip(pageIndex * 5).Take(5);
            string editlink = "<a href='/VideoManager/Edit/{0}'>Edit</a>";
            string deletelink = "<a href='/VideoManager/Delete/{0}'>Delete</a>"; 
            foreach (var video in videos)
            {
                html += "<tr><td>" + video.Title + "</td><td>" + video.Review + "</td><td>" + video.Year.ToString() +"</td><td>"+string.Format(editlink,video.Id)+"</td><td>"+string.Format(deletelink,video.Id)+"</td><tr>";
            }
            return html;
        }

        //
        // GET: /VideoManager/Create

        public ActionResult Create()
        {
            Video video = new Video();
            List<SelectListItem> items = new List<SelectListItem>();
            items.Add(new SelectListItem { Text="2009",Value="2009"});
            items.Add(new SelectListItem {Text="2010",Value="2010" });
            ViewData["Years"] = items;
            return View(video);
        }


    }
}
